#!/usr/bin/python 
#-*-coding：utf-8 -*- 

'''
# -------------------------------------------------------------------------------
# Purpose:     
#
# Author:      青衫少年春归也
#
# Created:     
#
# Copyright:   
# -------------------------------------------------------------------------------
'''

import re
import requests
import subprocess
from pathlib import Path
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

class youkuVideoDownload():
    def __init__(self, source:str):
        # 加启动配置
        option = webdriver.ChromeOptions()
        option.add_argument('disable-infobars')
        # 后台运行，静默执行，无界面弹出
        # option.add_argument('headless')
        self.browser = webdriver.Chrome(options=option)
        self.download_list = []
        self.source =source

    def __del__(self):
        self.browser.close()

    def start(self, start_url:str):
        """
        启动爬虫
            :param start_url:
        :return:
        """
        try:
            self.browser.get(start_url)
            self.get_download_link()
            page_up_items = self.browser.find_elements_by_xpath("//div[@class='label-text_2OL0W']/..")
            for page in page_up_items[1:]:
                try:
                    int(page.text)
                except:
                    continue
                page.click()
                self.get_download_link()
            self.download()
        except Exception as error:
            print('error', error)

    def get_download_link(self):
        """获取下载地址"""
        WebDriverWait(self.browser, 20, 0.5).until(EC.presence_of_element_located((By.CLASS_NAME, 'pack-right_ToYBg')))
        items = self.browser.find_elements_by_css_selector('.pack-right_ToYBg a')
        for item in items:
            href = item.get_attribute('href')
            if href.find('javascript:void') == -1:
                self.download_list.append({"url": f'{href}', 'source': self.source})

    def download(self):
        Path(f'./{self.source}').mkdir(parents=True, exist_ok=True)
        for link in self.download_list:
            url = link.get('url', '')
            try:
                print(f"\n ----    正在下载链接：{url}      -----\n")
                s = subprocess.Popen(f'you-get {url} -o {self.source}')
                s.communicate(timeout=600)
            except Exception as error:
                print(f'ERROR:{error}')


class souhuVideoDoenload():
    def __init__(self, source:str):
        # 加启动配置
        option = webdriver.ChromeOptions()
        option.add_argument('disable-infobars')
        # 后台运行，静默执行，无界面弹出
        # option.add_argument('headless')
        self.browser = webdriver.Chrome(options=option)
        self.download_list = []
        self.source =source

    def __del__(self):
        self.browser.close()

    def start(self, start_url:str):
        """
        启动爬虫
            :param start_url:
        :return:
        """
        try:
            self.browser.get(start_url)
            search = self.browser.find_element_by_id("gNewSearch")
            search.clear()
            search.send_keys("火灾")
            submit = self.browser.find_element_by_xpath('//a[@class="hd-submit"]')
            submit.click()
            self.browser.switch_to.window(self.browser.window_handles[1])
            self.get_download_link()
            page = 1
            try:
                while page:
                    page = self.browser.find_element_by_xpath("//div[@class='ssPages area']/a[text()='下一页']")
                    page.click()
                self.get_download_link()
            except Exception as error:
                print(error)
            self.download()
        except Exception as error:
            print('error', error)

    def get_download_link(self):
        """获取下载地址"""
        # WebDriverWait(self.browser, 20, 0.5).until(EC.presence_of_element_located((By.CLASS_NAME, 'pic170')))
        WebDriverWait(self.browser, 5)
        items = self.browser.find_elements_by_css_selector('.pic170 a')
        for item in items:
            href = item.get_attribute('href')
            if href.find('javascript:void') == -1:
                self.download_list.append({"url": f'{href}', 'source': self.source})

    def download(self):
        Path(f'./{self.source}').mkdir(parents=True, exist_ok=True)
        for link in self.download_list:
            url = link.get('url', '')
            try:
                print(f"\n ----    正在下载链接：{url}      -----\n")
                s = subprocess.Popen(f'you-get {url} -o {self.source}')
                s.communicate(timeout=600)
            except Exception as error:
                print(f'ERROR:{error}')

class pptvVideoDownload():
    def __init__(self, source:str):
        self.download_list = []
        self.source =source

    def start(self, start_url:str):
        """
        启动爬虫
            :param start_url:
        :return:
        """
        try:
            res = requests.get('http://sou.pptv.com/s_video?kw=%E7%83%9F%E9%9B%BE&context=default',
                               headers={"Cookie": 'logintp=0; idx_player_ap=1; PUID=7c07f0f7ce5a4a9cb9ea-8d94c615bce7; _snstyxuid=FFAC7292C5C9SKLI; ppi=302c3538; Hm_lvt_7adaa440f53512a144c13de93f4c22db=1590929048; __ssar=direct%7Cdirect%7C%7C%7C; _snvd=1590929048118saMaEI2qxXy; __crt=1590929048097; ppdi=eyJwbHQiOiJwYyIsIm1hYyI6IiIsIm1hYzEiOiIiLCJhbmRyb2lkaWQiOiIiLCJhbmRyb2lkaWQxIjoiIiwiaWRmYSI6IiIsIm9yaWdpbklwIjoiMTI1LjExOC4xMDQuMzcifQ; searchHistroy=%5B%22%E7%83%9F%E9%9B%BE%22%5D; SN_SESSION_ID=f6b67f7d-8e81-4e92-ac84-8b3f1e8b3410; __ssaf=http%3A%2F%2Fsou.pptv.com%2Fs_video%3Fkw%3D%25E7%2583%259F%25E9%259B%25BE%26context%3Ddefault; __ssav=159092904799822892%7C1590929047998%7C1590929056550%7C1590929298559%7C3%7C1%7C1; __ssas=159092904800582410%7C1590929298570%7C1590929298561%7C3; Hm_lpvt_7adaa440f53512a144c13de93f4c22db=1590929299',
                                        "Host": "sou.pptv.com",
                                        "Referer": "http://www.pptv.com/",
                                        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"})
            response = res.content.decode('utf-8')
            urls = re.findall(r'<a href=\"(.*?)\"\s+target=\"_blank\"\s+class=\"img-block\"', response)
            print(urls)
            self.download_list.extend(urls)
            self.download()
        except Exception as error:
            print('error', error)


    def download(self):
        Path(f'./{self.source}').mkdir(parents=True, exist_ok=True)
        for link in self.download_list:
            url = f"http:{link}"
            try:
                print(f"\n ----    正在下载链接：{url}      -----\n")
                s = subprocess.Popen(f'you-get {url} -o {self.source} --debug')
                s.communicate(timeout=600)
            except Exception as error:
                print(f'ERROR:{error}')

class cctvVideoDownload():
    def __init__(self):
        # 加启动配置
        option = webdriver.ChromeOptions()
        option.add_argument('disable-infobars')
        # 后台运行，静默执行，无界面弹出
        # option.add_argument('headless')
        self.browser = webdriver.Chrome(options=option)
        self.download_list = []
        self.source = 'cctv'

    def __del__(self):
        self.browser.close()

    def start(self, start_url:str):
        """
        启动爬虫
            :param start_url:
        :return:
        """
        try:
            self.browser.get(start_url)
            WebDriverWait(self.browser, 20, 0.5).until(EC.presence_of_element_located((By.ID, 'search_qtext')))
            search = self.browser.find_element_by_id('search_qtext')
            search.clear()
            search.send_keys("烟雾")
            submit = self.browser.find_element_by_xpath('//a[@onclick="searchForm_submit();"]')
            submit.click()
            self.get_download_link()
            page = 1
            try:
                while page:
                    page = self.browser.find_element_by_xpath("//a[@class='page-next page_cur' and text()='下一页']")
                    page.click()
                    self.get_download_link()
            except:
                pass
            self.download()
        except Exception as error:
            print('error', error)

    def get_download_link(self):
        """获取下载地址"""
        WebDriverWait(self.browser, 20, 0.5).until(EC.presence_of_element_located((By.CLASS_NAME, 'jvedio')))
        items = self.browser.find_elements_by_xpath('//div[@class="jvedio"]/a')
        for item in items:
            href = item.get_attribute('lanmu1')
            if href.find('javascript:void') == -1:
                self.download_list.append({"url": f'{href}', 'source': self.source})

    def download(self):
        Path(r'I:\cctv').mkdir(parents=True, exist_ok=True)
        for link in self.download_list:
            url = link.get('url', '')
            try:
                print(f"\n ----    正在下载链接：{url}      -----\n")
                s = subprocess.Popen(f'you-get {url} -o "I:\cctv" -k --skip-existing-file-size-check')
                s.communicate(timeout=1200)
            except Exception as error:
                print(f'ERROR:{error}')

def youku():
    youku = youkuVideoDownload('youku')
    youku.start('https://so.youku.com/search_video/q_%E7%81%AB%E7%81%BE%20%E7%83%9F%E9%9B%BE')

def souhu():
    down = souhuVideoDoenload('souhu')
    down.start('https://tv.sohu.com')

def pptv():
    down = pptvVideoDownload('pptv')
    down.start('http://sou.pptv.com/s_video?kw=%E7%83%9F%E9%9B%BE&context=default')

if __name__ == '__main__':
    c = cctvVideoDownload()
    c.start('https://search.cctv.com/index.php')
